import React, { useState } from "react";

const TaskForm = ({ onAddTask }) => {
  const [inputValue, setInputValue] = useState("");

  const handleSubmit = (e) => {
    e.preventDefault();
    if (inputValue.trim() !== "") {
      onAddTask(inputValue);
      setInputValue("");
    }
  };

  const handleKeyPress = (e) => {
    if (e.key === "Enter") {
      handleSubmit(e);
    }
  };

  return (
    <form onSubmit={handleSubmit} className="mb-4">
      <div className="input-group">
        <input
          type="text"
          className="form-control"
          placeholder="输入新任务..."
          value={inputValue}
          onChange={(e) => setInputValue(e.target.value)}
          onKeyPress={handleKeyPress}
        />
        <button
          className="btn btn-primary"
          type="submit"
          disabled={inputValue.trim() === ""}
        >
          <i className="bi bi-plus-circle me-1"></i>
          添加任务
        </button>
      </div>
    </form>
  );
};

export default TaskForm;
